package com.prs.crm.action.oa;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.context.AppContext;
import com.prs.crm.domain.oa.Notice;
import com.prs.crm.domain.oa.NoticeCollect;
import com.prs.crm.domain.oa.NoticeCollectType;
import com.prs.crm.service.oa.NoticeCollectService;
import com.prs.crm.service.oa.NoticeCollectTypeService;

public class NoticeCollectAction extends BaseAction {
	private NoticeCollect noticeCollect;
	private NoticeCollectService noticeCollectService;
	private PaginationSupport pagination;
	private Integer[] ids;
	private Integer[] collectIds;
	private NoticeCollectType noticeCollectType;
	private NoticeCollectTypeService noticeCollectTypeService;
	private List<NoticeCollectType> noticeCollectTypes;
	
	@Action("listNoticeCollect")
	public String listNoticeCollect(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(NoticeCollect.class);
		criteria.add(Restrictions.eq("recorder.id", AppContext.getAccountContext().currentUser().getId()));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getNoticeCollectService().listByPage(criteria,pagination));
		return SUCCESS;
	}
	
	@Action(value = "removeNoticeCollect", results = {
			@Result(name = "success", type = "redirect", location = "listNoticeCollectOA"),
			@Result(name = "error", type = "chain", location = "listNoticeCollectOA") })
	public String removeNoticeCollect(){
		if(collectIds == null){
			this.addActionError("请选择要删除的通知收藏");
			return ERROR;
		}
		this.getNoticeCollectService().batchRemove(collectIds);
		return SUCCESS;
	}
	
	@Action(value = "editNoticeMove", results = {
			@Result(name = "error", type = "chain", location = "listCollectNews") })
		public String editNoticeMove(){
			if (collectIds!=null) {
				this.noticeCollect=this.getNoticeCollectService().get(collectIds[0]);
			}else{
				this.addActionError("请选择一行进行操作！");
				return ERROR;
			}
			noticeCollectTypes=this.getNoticeCollectTypeService().getAll();
			return SUCCESS;
		}
		@Action(value = "saveNoticeMove", results = {
				@Result(name = "success", type = "redirect", location = "listNoticeCollectOA"),
				@Result(name = "error", type = "chain", location = "editNoticeMove") })
		public String saveNoticeMove(){
			if (noticeCollect==null) {
				this.addActionError("请选择一项进行移动！");
				return ERROR;
			}
			this.getNoticeCollectService().submit(noticeCollect);
			return SUCCESS;
		}
	@Action("viewNoticeCollect")
	public String viewNoticeCollect(){
		return SUCCESS;
	}
	@Action("viewNoticeCollectOA")
	public String viewNoticeCollectOA(){
		
		return SUCCESS;
	}
	public NoticeCollect getNoticeCollect() {
		return noticeCollect;
	}
	public void setNoticeCollect(NoticeCollect noticeCollect) {
		this.noticeCollect = noticeCollect;
	}
	public NoticeCollectService getNoticeCollectService() {
		return noticeCollectService;
	}
	public void setNoticeCollectService(NoticeCollectService noticeCollectService) {
		this.noticeCollectService = noticeCollectService;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public Integer[] getCollectIds() {
		return collectIds;
	}

	public void setCollectIds(Integer[] collectIds) {
		this.collectIds = collectIds;
	}

	public NoticeCollectType getNoticeCollectType() {
		return noticeCollectType;
	}

	public void setNoticeCollectType(NoticeCollectType noticeCollectType) {
		this.noticeCollectType = noticeCollectType;
	}

	public NoticeCollectTypeService getNoticeCollectTypeService() {
		return noticeCollectTypeService;
	}

	public void setNoticeCollectTypeService(
			NoticeCollectTypeService noticeCollectTypeService) {
		this.noticeCollectTypeService = noticeCollectTypeService;
	}

	public List<NoticeCollectType> getNoticeCollectTypes() {
		return noticeCollectTypes;
	}

	public void setNoticeCollectTypes(List<NoticeCollectType> noticeCollectTypes) {
		this.noticeCollectTypes = noticeCollectTypes;
	}
}
